package com.wwy.arithmetic.suanfa;

import com.wwy.arithmetic.suanfa.entry.ListNode;
import lombok.Data;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 环形链表
 */
public class HuanXingLianBiao {


    public static void main(String[] args) {
        ListNode node = new ListNode(3);
//        ListNode node1 = new ListNode(2);
//        ListNode node2 = new ListNode(0);
//        ListNode node3 = new ListNode(4);
//        node.setNext(node1);
//        node1.setNext(node2);
//        node2.setNext(node3);
//        node3.setNext(node1);
        int judge = judge(node);
        System.out.println("judge = " + judge);
    }

    static int judge(ListNode node) {  //3 2 0 4 2 0 4
        Map<Object, Integer> map = new HashMap<>();
        int i = 0;
        while (node != null) {
            if (map.containsKey(node)) {
                return map.get(node);
            } else {
                map.put(node, i);
                i++;
            }
            node = node.getNext();
        }
        return -1;
    }
}
